<?PHP
session_start();
require(substr($_SERVER['SCRIPT_FILENAME'],0,-37).'stationInfo.php');
require_once('../../config.php');


//Get rid of all existing text files in directory
foreach (glob("*.txt") as $filename) {
   //echo "$filename size " . filesize($filename) . "\n";
   unlink($filename);
}


$file = strtoupper($stationID)."_".strtoupper(date("D")).'.txt';
$filePointer = fopen($file, "w+");

for ($i = 0; $i < count($_SESSION['merged_array']); $i++) {
	if(!$_SESSION['merged_array'][$i]['ID'] && $_SESSION['merged_array'][$i]['type'] != 'PSA'){//we have a Spot
		$time = date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . "" .date("A",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0));
		$automation = "|".$time."|+|".$_SESSION['merged_array'][$i]['spotID']."|UW Credit|"."\n";
		fwrite ($filePointer,$automation);
	}elseif($_SESSION['merged_array'][$i]['type'] == 'PSA' && $_SESSION['merged_array'][$i]['title'] != '' && $_SESSION['merged_array'][$i]['title'] != '&nbsp;'){//We have a PSA
		$time = date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . "" .date("A",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0));
		$automation = "|".$time."|+|".$_SESSION['merged_array'][$i]['title']."|PSA|"."\n";
		fwrite ($filePointer,$automation);
	}elseif($_SESSION['merged_array'][$i]['type'] == 'EPSA'){//We have a EPSA
		$time = date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . "" .date("A",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0));
		$automation = "|".$time."|+|".$_SESSION['merged_array'][$i]['spotID']."|EPSA|"."\n";
		fwrite ($filePointer,$automation);
	}elseif($_SESSION['merged_array'][$i]['type'] == 'SPA' && $_SESSION['merged_array'][$i]['title'] != '' && $_SESSION['merged_array'][$i]['title'] != '&nbsp;'){//We have a SPA
		$time = date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . "" .date("A",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0));
		$automation = "|".$time."|+|".$_SESSION['merged_array'][$i]['title']."|SPA|"."\n";
		fwrite ($filePointer,$automation);
	}
}

 

//fwrite ($filePointer, "boogers are great YO\n");
//fwrite ($filePointer, "But ya still gotta pick 'em");



fclose ($filePointer); 
//print_r($_SESSION['merged_array']); 
header("Location:$file");
?>

